package org.example;

import org.example.exam.AbstractExam;
import org.example.exam.Paper;
import org.example.exam.compenet.PaperLoad;
import org.example.spring.SpringUtil;

import java.text.MessageFormat;
import java.util.Collections;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Hello world!
 *
 */
public class ExamApp {

    private static final Logger logger = Logger.getLogger(ExamApp.class.getName());

    public static void main( String[] args ) {
        PaperLoad paperLoad = SpringUtil.getBean(PaperLoad.class);
        paperLoad.loadPapers("paper");
        String cmd;
        do {
            logger.info("请输入试卷序号进行选择 >>> ");
            for (int i = 0; i < PaperLoad.papers.size(); i++) {
                Paper paper = PaperLoad.papers.get(i);
                logger.info(i + ":" + paper.getTitle());
            }
            Scanner input = new Scanner(System.in);
            Paper paper = null;
            boolean pass = false;
            do {
                String val = input.next();
                try {
                    int paperIndex = Integer.parseInt(val);
                    paper = PaperLoad.papers.get(paperIndex);
                    pass = true;
                } catch (NumberFormatException e) {
                    logger.warning("请输入试卷序号进行选择！");
                } catch (IndexOutOfBoundsException e) {
                    logger.warning("试卷需要不存在，请重新输入！");
                }
            } while(!pass);
            logger.info("已选择试卷：" + paper.getTitle() + ", 总共" + paper.getExamQuestion().size() +"道题，请开始作答！");
            double totalScore = 0f;
            double getScore = 0f;
            // 打乱题目顺序
            Collections.shuffle(paper.getExamQuestion());
            for (int i = 0; i < paper.getExamQuestion().size(); i++) {
                logger.log(Level.INFO, MessageFormat.format("\n>>>>>>>>>>>>>>>>>>>>>>> 第 {0} 题 >>>>>>>>>>>>>>>>>>>>>>>", i + 1));
                AbstractExam abstractExam = paper.getExamQuestion().get(i);
                totalScore += abstractExam.score;
                logger.info(abstractExam.printTitle());
                String answer = input.next();
                double score = abstractExam.correctTheExaminationPaperAndPrint(answer);
                getScore += score;
            }
            logger.log(Level.INFO, "========================完成作答========================");
            logger.info("作答完成，卷面总分：" + totalScore + "，答题得分：" + getScore);
            logger.log(Level.INFO, "\n========================输入指令========================\n1.输入 c 重新选择试卷\n2.输入 c 以外任意字符结束程序");
            cmd = input.next();
        } while ("c".equals(cmd));
    }
}
